package cn.tedu.service.impl;

import cn.tedu.mapper.UserMapper;
import cn.tedu.pojo.User;
import cn.tedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public String login(User user) {
        try {
            User userExistN = userMapper.findByName(user.getUsername());
            if (userExistN != null) {
                String userExistP = userMapper.findPswByName(user.getUsername());
                if (userExistP.equals(user.getPassword())) {
                    return user.getUsername() + "登录成功，欢迎您！";
                } else {
                    return "登录失败，密码错误！";
                }
            } else {
                return "登录失败,用户不存在。";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }

    }

    @Override
    public String register(User user) {
        try {
            User userExist = userMapper.findByName(user.getUsername());
            if (user.getUsername().equals("")) {
                return "用户名不能为空";
            } else if (user.getPassword().equals("")) {
                return "密码不能为空";
            } else if (userExist != null) {
                return "账号已存在";
            } else {
                userMapper.save(user);
                return "注册成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
}
